<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="渠道代码" prop="channelCode">
        <el-input v-model="queryParams.channelCode" placeholder="请输入渠道代码" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <!--  <el-form-item label="保单号" prop="policyNum">
        <el-input v-model="queryParams.policyNum" placeholder="请输入保单号" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item> -->
      <!--   <el-form-item label="车牌号" prop="plateNum">
        <el-input v-model="queryParams.plateNum" placeholder="请输入车牌号" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item> -->
      <el-form-item label="vin" prop="vin">
        <el-input v-model="queryParams.vin" placeholder="请输入vin" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="销售车型" prop="amVehicleName">
        <el-input v-model="queryParams.amVehicleName" placeholder="请输入销售车型名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="车主姓名" prop="ownerName">
        <el-input v-model="queryParams.ownerName" placeholder="请输入车主姓名" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="车主电话" prop="ownerPhone">
        <el-input v-model="queryParams.ownerPhone" placeholder="请输入车主电话" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>

      <el-form-item label="保单日期" prop="policyDate">
        <el-date-picker v-model="queryParams.policyDate" value-format="YYYY-MM-DD HH:mm:ss" class="!w-240px" />
      </el-form-item>
      <el-form-item label="安装次数" prop="installNum">
        <el-input v-model="queryParams.installNum" placeholder="请输入安装次数" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="openForm('create')">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button type="warning" plain @click="handleImport">
          <Icon icon="ep:upload" /> 导入
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" border :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="渠道代码" align="center" prop="channelCode" width="100" />
      <el-table-column label="保单号" align="center" prop="policyNum" width="100" />
      <el-table-column label="车牌号码" align="center" prop="plateNum" width="100" />
      <el-table-column label="vin" align="center" prop="vin" width="200" />
      <el-table-column label="销售车型" align="center" prop="amVehicleName" width="120" />
      <el-table-column label="保单日期" align="center" prop="policyDate" width="100" />
      <el-table-column label="保单生效日期" align="center" prop="effectDate" width="120" />
      <el-table-column label="保单截止日期" align="center" prop="cutDate" width="120" />
      <el-table-column label="车主姓名" align="center" prop="ownerName" width="100" />
      <el-table-column label="车主电话" align="center" prop="ownerPhone" width="100" />
      <el-table-column label="车主地址" align="center" prop="ownerAddress" width="200" />
      <el-table-column label="安装次数" align="center" prop="installNum" width="100" />
      <el-table-column label="报案" align="center" prop="reportAddress" width="200" />
      <el-table-column label="报案详情" align="center" prop="reportInfo" width="100" />
      = <el-table-column label="操作" align="center" width="200" fixed="right"><template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <OwnerForm ref="formRef" @success="getList" />
  <ImportForm ref="importFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { OwnerApi, OwnerVO } from '@/api/channels/owner'
import OwnerForm from './OwnerForm.vue'
import ImportForm from './ImportForm.vue'
/** 保单-车主信息 列表 */
defineOptions({ name: 'ErpOwner' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<OwnerVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  channelCode: undefined,
  policyNum: undefined,
  plateNum: undefined,
  vin: undefined,
  amVehicleId: undefined,
  amVehicleName: undefined,
  ownerName: undefined,
  ownerPhone: undefined,
  ownerAddress: undefined,
  effectDate: [],
  cutDate: [],
  frontImg: undefined,
  tailImg: undefined,
  leftImg: undefined,
  rightImg: undefined,
  frontPlace: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
/** 导入 */
const importFormRef = ref()
const handleImport = () => {
  importFormRef.value.open()
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OwnerApi.getOwnerPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await OwnerApi.deleteOwner(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await OwnerApi.exportOwner(queryParams)
    download.excel(data, '保单-车主信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
